package server;

import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

import data.Player;

public class CocaroHandler implements IoHandler
{
	
	public CocaroHandler()
	{
	
	}
	@Override
	public void exceptionCaught(IoSession arg0, Throwable arg1)
			throws Exception
	{

		
	}

	@Override
	public void messageReceived(IoSession ioSession, Object object) throws Exception
	{
		System.out.println(ioSession.getId() + "gui len server: " + object.toString());
		if (ioSession.isConnected())
		{
			System.out.println("session connected");
			ioSession.write("fdsafsa");

		}
		else
		{
			System.out.println("session disconnected" + ioSession.isConnected());
		}
//		
	}

	@Override
	public void messageSent(IoSession ioSession, Object object) throws Exception
	{
		System.out.println("sending..." + object);
	}

	@Override
	public void sessionClosed(IoSession ioSession) throws Exception
	{
		// TODO remove ioSession khoi mang Server.sessions
		// TODO remove khoi mang nay dong thoi remove khoi du lieu trong Server.rooms
	}

	@Override
	public void sessionCreated(IoSession arg0) throws Exception
	{


		
	}

	@Override
	public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception
	{
	
		
	}

	@Override
	public void sessionOpened(IoSession ioSession) throws Exception
	{

		
		ioSession.setAttribute("player", null);
		
		Server.sessions.put(ioSession.getId(), ioSession);
		System.out.println("so sessions="+Server.sessions.size());
		System.out.println("sessionID=" + ioSession.getId());
	}

}
